package com.antdesignbackend.backend.news.news.service.impl;


import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.news.news.entity.entityVo.NewsVo;
import com.antdesignbackend.backend.news.newsusers.entity.NewsUsers;
import com.antdesignbackend.backend.news.newsusers.service.NewsUsersService;
import com.antdesignbackend.backend.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.news.news.mapper.NewsMapper;
import com.antdesignbackend.backend.news.news.entity.News;
import com.antdesignbackend.backend.news.news.service.NewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    NewsMapper newsMapper;

    @Autowired
    NewsService newsService;

    @Autowired
    UserService userService;

    @Autowired
    NewsUsersService newsUsersService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void createNews(News news) throws Exception{
        //获得发布人的权限区域
        Users u = userService.getById(news.getCreateUid());
        if (u != null){
            news.setAuthCity(u.getAuthCity());
            news.setAuthDistrict(u.getAuthDistrict());
            news.setAuthTown(u.getAuthTown());
        }
        if(newsService.save(news)){
            //根据角色查询到对应的用户并添加到用户消息列表
            /**
             * 养殖企业单独判断
             */
            String[] s = news.getRoles().split(",");
            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            //判断用户权限
            if (u.getAuthType() == 1){ //地市权限
                queryWrapper.eq("AuthCity",u.getAuthCity());
            }else if (u.getAuthType() == 2){ //区县权限
                queryWrapper.eq("AuthDistrict",u.getAuthDistrict());
            }else if (u.getAuthType() == 3){
                queryWrapper.eq("AuthTown",u.getAuthTown());
            }
            queryWrapper.in("userType", s);
            List<Users> list = userService.list(queryWrapper);
            if (list.size() > 0){
                List<NewsUsers> newsUsersList = new ArrayList<>();
                for (Users users:list){
                    NewsUsers newsUsers = new NewsUsers();
                    newsUsers.setNid(news.getId());
                    newsUsers.setUid(users.getUserID());
                    newsUsers.setCreateTime(new Date());
                    newsUsers.setStatus(0);
                    newsUsersList.add(newsUsers);
                }
                newsUsersService.saveBatch(newsUsersList);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateNews(News news) throws Exception {
        //获得发布人的权限区域
        Users u = userService.getById(news.getCreateUid());
        if(newsService.updateById(news)){
            //删除之前的
            QueryWrapper<NewsUsers> wrapper = new QueryWrapper<>();
            wrapper.eq("nid",news.getId());
            newsUsersService.remove(wrapper);
            //根据角色查询到对应的用户并添加到用户消息列表
            /**
             * 养殖企业单独判断
             */
            String[] s = news.getRoles().split(",");

            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            //判断用户权限
            if (u.getAuthType() == 1){ //地市权限
                queryWrapper.eq("AuthCity",u.getAuthCity());
            }else if (u.getAuthType() == 2){ //区县权限
                queryWrapper.eq("AuthDistrict",u.getAuthDistrict());
            }else if (u.getAuthType() == 3){
                queryWrapper.eq("AuthTown",u.getAuthTown());
            }
            queryWrapper.in("userType", s);
            List<Users> list = userService.list(queryWrapper);
            if (list.size() > 0){
                List<NewsUsers> newsUsersList = new ArrayList<>();
                for (Users users:list){
                    NewsUsers newsUsers = new NewsUsers();
                    newsUsers.setNid(news.getId());
                    newsUsers.setUid(users.getUserID());
                    newsUsers.setCreateTime(new Date());
                    newsUsers.setStatus(0);
                    newsUsersList.add(newsUsers);
                }
                newsUsersService.saveBatch(newsUsersList);
            }
        }
    }

    @Override
    public List<NewsVo> search(QueryWrapper<NewsVo> wrapper, Page<NewsVo> page) {
        return newsMapper.search(wrapper,page);
    }

    @Override
    public Integer count(QueryWrapper<NewsVo> wrapper) {
        return newsMapper.count(wrapper);
    }
}
